package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.vo.ContractProductVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ContractProductServiceImpl implements ContractProductService {

    @Autowired
    private ContractProductDao  contractProductDao;

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ExtCproductDao extCproductDao;

    @Override
    public PageInfo<ContractProduct> findByPage(ContractProductExample ContractProductExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ContractProduct> factoryList = contractProductDao.selectByExample(ContractProductExample);
        PageInfo<ContractProduct> pageInfo = new PageInfo<>(factoryList);
        return pageInfo;
    }

    @Override
    public List<ContractProduct> findAll(ContractProductExample ContractProductExample) {
        return contractProductDao.selectByExample(ContractProductExample);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(ContractProduct contractProduct) {
        //1、计算货物的总价
        double amount=0.0;
        //由于单价与数量我们都使用了包装类型，所以可以使用null进行判断
        if(contractProduct.getPrice()!=null && contractProduct.getCnumber()!=null) {
            amount = contractProduct.getPrice() * contractProduct.getCnumber();
            contractProduct.setAmount(amount);
        }

        //2、插入货物的数据
        contractProduct.setId(UUID.randomUUID().toString());
        contractProduct.setCreateTime(new Date());
        contractProduct.setUpdateTime(new Date());
        contractProductDao.insertSelective(contractProduct);

        //3、购销合同的货物的种类数量+1
        String contractId = contractProduct.getContractId();//得到购销合同的id
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        if (contract.getProNum()!=null){
            contract.setProNum(contract.getProNum()+1);
        }else {
            contract.setProNum(1);
        }


        //4、更新购物合同的总价（加上货物的总价）
        if (contract.getTotalAmount()!=null){
            contract.setTotalAmount(contract.getTotalAmount()+amount);
        }else {
            contract.setTotalAmount(amount);
        }

        //5、更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public void update(ContractProduct contractProduct) {
        contractProduct.setUpdateTime(new Date());

        //得到购销合同对象
        String contractId = contractProduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        //货物原本价格
        //更新之前查询该货物的对象
        ContractProduct oldContractProduct = contractProductDao.selectByPrimaryKey(contractProduct.getId());
        Double oldAmount = oldContractProduct.getAmount();

        //1、计算货物的总价
        double amount=0.0;
        //由于单价与数量我们都使用了包装类型，所以可以使用null进行判断
        if(contractProduct.getPrice()!=null && contractProduct.getCnumber()!=null) {
            amount = contractProduct.getPrice() * contractProduct.getCnumber();
            contractProduct.setAmount(amount);
        }

        //2. 更新货物数据
        contractProductDao.updateByPrimaryKeySelective(contractProduct);

        //3. 重新计算购销合同的总价   购销合同总价 = 购销合同原价格- 货物原本价格+ 当前的新总价
        contract.setTotalAmount(contract.getTotalAmount() - oldAmount + amount);

        //4. 更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public void delete(String id) {
        //删除前得到被删除货物对象
        ContractProduct dbContractProduct = contractProductDao.selectByPrimaryKey(id);
        //得到购销合同对象
        Contract contract = contractDao.selectByPrimaryKey(dbContractProduct.getContractId());

        //1. 删除货物
        contractProductDao.deleteByPrimaryKey(id);

        //2. 删除该货物对应的附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractProductIdEqualTo(id);

        List<ExtCproduct> extCproductList = extCproductDao.selectByExample(extCproductExample);

        //定义一个变量来接收被删除附件的总价
        double extTotalAmount = 0;
        if (extCproductList != null) {
            for (ExtCproduct extCproduct : extCproductList) {
                extTotalAmount += extCproduct.getAmount();
                extCproductDao.deleteByPrimaryKey(extCproduct.getId());
            }
        }

        //3.  更新购销合同的总价   更新购销合同 = 购销合同原总价-被删除货物的总价- 附件的总价
        contract.setTotalAmount(contract.getTotalAmount() - dbContractProduct.getAmount() - extTotalAmount);
        //4. 更新购销合同的货物数量
        contract.setProNum(contract.getProNum() - 1);

        //5. 更新购销合同的附件数量
        if (extCproductList != null) {
            contract.setExtNum(contract.getExtNum() - extCproductList.size());
        }
        //6. 更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public List<ContractProductVo> findByShipTime(String shipTime, String companyId) {
        return contractProductDao.findByShipTime(shipTime, companyId);
    }
}
